﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace CRMSalesManagement.Manage.FrontPageManagement
{
    public partial class ProductModify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["productid"] != null)
                {
                    int productID = Convert.ToInt32(Session["productid"]);
                    var member =CRM.Business.Product.GetNewById(productID);
                    if (member != null)
                    {
                        this.txtModel.Value = member.model;
                        this.ddlType.Text = member.type;
                        this.ddlBrand.Text = member.brand;
                        this.txtInnerDiameter.Value = member.innerDiameter.ToString();
                        this.txtExternalDiameter.Value = member.externalDiameter.ToString();
                        this.txtHeight.Value = member.height.ToString();
                        this.txtLimitSpeed.Value = member.limitSpeed.ToString();
                        this.txtReferenceSpeed.Value = member.referenceSpeed.ToString();
                        this.imgProduct.ImageUrl = member.image;                        
                        Session["savePath"] = member.image;
                    }
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            { 
                if (txtExternalDiameter.Value == "" || txtHeight.Value == "" || txtInnerDiameter.Value == "" || txtLimitSpeed.Value == "" || txtModel.Value == "" || txtReferenceSpeed.Value == "")
                {
                    lblSaveResute.Text = "带*的内容不能为空";
                }
                else
                {
                    if (Convert.ToInt32(this.txtInnerDiameter.Value.Trim())>=Convert.ToInt32(this.txtExternalDiameter.Value.Trim()))
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('轴承参数错误：内径不能大于或等于外径！');</script>"); 
                        // Response.Write("<script>alert('轴承参数错误：内径不能大于或等于外径！')</script>");
                    }
                    else
                    {
                        if (Convert.ToInt32(this.txtReferenceSpeed.Value.Trim())>= Convert.ToInt32(this.txtLimitSpeed.Value.Trim()))
                        {
                            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('轴承参数错误：参考转速不能大于或等于极限转速！');</script>");
                            //Response.Write("<script>alert('轴承参数错误：参考转速不能大于或等于极限转速！')</script>");
                        }
                        else
                        {
                            if (Session["productid"] != null)
                            {
                                int productID = Convert.ToInt32(Session["productid"]);
                                var product = CRM.Business.Product.GetNewById(productID);
                                if (product != null)
                                {
                                    product.model = this.txtModel.Value.Trim();
                                    product.type = this.ddlType.Text;
                                    product.brand = this.ddlBrand.Text;
                                    product.innerDiameter = Convert.ToInt32(this.txtInnerDiameter.Value.Trim());
                                    product.externalDiameter = Convert.ToInt32(this.txtExternalDiameter.Value.Trim());
                                    product.height =Convert.ToInt32( this.txtHeight.Value.Trim());
                                    product.limitSpeed = Convert.ToInt32(this.txtLimitSpeed.Value.Trim());
                                    product.referenceSpeed = Convert.ToInt32(this.txtReferenceSpeed.Value.Trim());
                                    product.image = Session["savePath"].ToString();
                                    bool goodAdd = CRM.Business.Product.ModifyProduct(product);
                                    if (!goodAdd)
                                    {
                                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('修改失败，请重试……');</script>"); 
                                        //Response.Write("<script>alert('修改失败，请重试……')</script>");
                                    }
                                    else
                                    {
                                        Response.Redirect("ProductsList.aspx");
                                    }
                                }
                            }
                        }
                    } 
                }
            }
            catch (Exception)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('修改失败，请检查您输入的是否合法……');</script>");                                        
            }
        }

        protected void btnPutIn_Click(object sender, EventArgs e)
        {
            string savePath = "";
            if (this.fulBrowser.HasFile)
            {
                string fileExt;
                fileExt = System.IO.Path.GetExtension(fulBrowser.FileName);
                if (fileExt == ".jpg" || fileExt == ".jpeg" || fileExt == ".png" || fileExt == ".gif")
                {
                    //  string fileContentType = fulBrowser.PostedFile.ContentType; //获取图片类型
                    savePath = Server.MapPath("~/images/ProductsPhotos/");
                    if (!Directory.Exists(savePath))
                    {
                        this.lblPutIn.Text = "找不到路径";
                    }
                    else
                    {
                        savePath += fulBrowser.FileName;
                        HttpPostedFile upFile = this.fulBrowser.PostedFile;
                        //获取上传文件大小，以字节为单位
                        int fileLength = upFile.ContentLength;
                        if (fileLength > 150 * 1024)
                        {
                            this.lblPutIn.Text = "图片大小不能超过150K";
                        }
                        else
                        {
                            fulBrowser.SaveAs(savePath);
                            this.imgProduct.ImageUrl = ("~/images/ProductsPhotos/") + this.fulBrowser.FileName;
                            Session["savePath"] = ("~/images/ProductsPhotos/") + this.fulBrowser.FileName;
                            this.lblPutIn.Text = "上传成功";
                        }
                    }
                }
                else
                {
                    this.lblPutIn.Text = "图片格式错误：只允许上传jpg、jpeg、png、gif文件";
                }
            }
        }
    }
}